from flask import Flask, request, jsonify
from openai import OpenAI

app = Flask(__name__)

# 初始化 OpenAI 客户端
client = OpenAI(api_key="sk-xxxxxxxxxx", base_url="https://api.deepseek.com")

# 聊天接口
@app.route('/chat', methods=['POST'])
def chat():
    # 获取用户输入
    data = request.json
    message = data.get('message', '')

    print(f"收到消息: {message}")

    # 调用 OpenAI API
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "You are a helpful assistant"},
            {"role": "user", "content": message},
        ],
    )

    # 返回 AI 回复
    return jsonify({
        'message': response.choices[0].message.content
    })

# 启动服务器
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)